package com.daxia.bumall.dto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.collections.CollectionUtils;

import com.daxia.bumall.basemodel.CoreUser;
import com.daxia.bumall.common.UserType;
import com.daxia.bumall.dto.api.ProductPropertiesAPIDTO;
import com.daxia.bumall.model.Product;
import com.daxia.bumall.model.Specification;
import com.daxia.bumall.service.UserService;
import com.daxia.bumall.support.SpringContextHolder;
import com.daxia.bumall.util.MyWebUtils;

public class ProductDTO extends Product {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Specification firstSpec;
	
	private Double minPrice;
	private Double maxPrice;
	
	private Double startPrice;   //查询传入的开始价格
	private Double endPrice;     //查询传入的结束价格
	
	private String exactName;
	private boolean isAllStatus;
	private boolean isFindByRandom;
	private List<ProductPropertiesAPIDTO> productPropertiesInfos;  
	private Specification defaultSpec;  //默认的规格，可以从前台传过来并封装，也可以自己设定

	private Map<String, String> filterMap = new HashMap<String, String>();
	private List<Long> brandIds = new ArrayList<Long>();
	private Integer sortType;   //排序方式
	
	private List<Long> liveDirectBrandIds = new ArrayList<Long>();
	private String searchTerm;
	private boolean isFromAdmin;
	
	private List<Long> filterIds;    //需要过滤查询的品牌ids，是在详情页中需要过滤掉商品本身 
	
	/*
	 * 2017-01-09 由于涉及到新的搜索形式，如：产品分类+颜色，产品分类+风格 产品分类+材质，需要从分类信息和属性信息里联合查询
	 */
	private String categoryName;    
	private List<String> propertyNames; 
	
	private String orderBy;
	private String style;
	
	public Specification getFirstSpec() {
		if (CollectionUtils.isNotEmpty(getSpecifications())) {
			return getSpecifications().get(0);
		}
		return firstSpec;
	}
		
	public Specification getDefaultSpec() {
		if(defaultSpec != null) {
			return defaultSpec;
		} else {
			return this.getFirstSpec();
		}
	}

	public void setDefaultSpec(Specification spec) {
		this.defaultSpec = spec;
	}
	
	public void setDefaultSpec(Long specId) {  //传入一个规格id，来设置默认的规格
		//this.defaultSpec = defaultSpec;
		if(specId != null) {
			List<Specification> specifications = this.getSpecifications();
			for(Specification theSpec:specifications) {
				if(specId.longValue() == theSpec.getId().longValue()) { 
					this.defaultSpec = theSpec;
					break;
				}
			}
		}
	}

	public void setFirstSpec(Specification firstSpec) {
		this.firstSpec = firstSpec;
	}

	public Double getMinPrice() {
		Double min = Double.MAX_VALUE;
		if (CollectionUtils.isNotEmpty(getSpecifications())) {
			for (Specification s : getSpecifications()) {
				if (s.getRealPrice() < min) {
					min = s.getRealPrice();
				}
			}
		}
		return min;
	}

	public void setMinPrice(Double minPrice) {
		this.minPrice = minPrice;
	}

	public Double getMaxPrice() {
		Double max = 0d;
		if (CollectionUtils.isNotEmpty(getSpecifications())) {
			for (Specification s : getSpecifications()) {
				if (s.getRealPrice() > max) {
					max = s.getRealPrice();
				}
			}
		}
		return max;
	}

	public void setMaxPrice(Double maxPrice) {
		this.maxPrice = maxPrice;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getExactName() {
		return exactName;
	}

	public void setExactName(String exactName) {
		this.exactName = exactName;
	}

	public boolean isAllStatus() {
		return isAllStatus;
	}

	public void setAllStatus(boolean isAllStatus) {
		this.isAllStatus = isAllStatus;
	}

	public boolean isFindByRandom() {
		return isFindByRandom;
	}

	public void setFindByRandom(boolean isFindByRandom) {
		this.isFindByRandom = isFindByRandom;
	}

	public List<ProductPropertiesAPIDTO> getProductPropertiesInfos() {
		/*
		List<ProductPropertiesAPIDTO> list = new ArrayList<ProductPropertiesAPIDTO>();
		List<ProductProperties> properties = super.getProductProperties();
		for (ProductProperties p : properties) {
			ProductPropertiesAPIDTO dto = new ProductPropertiesAPIDTO();
			dto.setName(p.getName());
			dto.setValue(p.getValue());
			list.add(dto);
		}
		*/
		throw new RuntimeException("数据结构变更，请重新实现");
		//return list;
	}

	public void setProductPropertiesInfos(List<ProductPropertiesAPIDTO> productPropertiesInfos) {
		this.productPropertiesInfos = productPropertiesInfos;
	}

	public Map<String, String> getFilterMap() {
		return filterMap;
	}

	public void setFilterMap(Map<String, String> filterMap) {
		this.filterMap = filterMap;
	}

	public List<Long> getBrandIds() {
		return brandIds;
	}

	public void setBrandIds(List<Long> brandIds) {
		this.brandIds = brandIds;
	}

	public Integer getSortType() {
		return sortType;
	}

	public void setSortType(Integer sortType) {
		this.sortType = sortType;
	}

	public Double getStartPrice() {
		return startPrice;
	}

	public void setStartPrice(Double startPrice) {
		this.startPrice = startPrice;
	}

	public Double getEndPrice() {
		return endPrice;
	}

	public void setEndPrice(Double endPrice) {
		this.endPrice = endPrice;
	}
	
	public Double getPriceFactor() {  //价格系数
		if (MyWebUtils.getCurrentRequest() == null) {
			return 1d;
		}
		UserService userService = SpringContextHolder.getBean(UserService.class);
		CoreUser coreUser = (CoreUser) MyWebUtils.getCurrentRequest().getSession().getAttribute("currentUser");
		if (coreUser == null) { // 没有登陆
			return 2d;
		}
		
		UserDTO user = userService.load(coreUser.getId());
		// 如果用户本身的不为空就返回本身的
		if (user.getPriceFactor() != null) {
			return user.getPriceFactor();
		}
		// 如果部门不为空
		if (user.getDepartment() != null && user.getDepartment().getPriceFactor() != null) {
			return user.getDepartment().getPriceFactor();
		}
				
		if (user.getType() == null) { // 未知类型
			return 2d;
		}
		if (user.getType() == UserType.City.getValue()) {
			return 1d;
		}
		if (user.getType() == UserType.DecorateCompany.getValue()) {
			return 1.06d;
		}
		if (user.getType() == UserType.Designer.getValue()) {
			return 1.15d;
		}
		if (user.getType() == UserType.Normal.getValue()) {
			return 2d;
		}
		if (user.getType() == UserType.Manager.getValue()) {
			return 1d; // 管理员看到出厂价
		}
		return 2d;
	}

	public List<Long> getLiveDirectBrandIds() {
		return liveDirectBrandIds;
	}

	public void setLiveDirectBrandIds(List<Long> liveDirectBrandIds) {
		this.liveDirectBrandIds = liveDirectBrandIds;
	}

	public String getSearchTerm() {
		return searchTerm;
	}

	public void setSearchTerm(String searchTerm) {
		this.searchTerm = searchTerm;
	}

	public boolean isFromAdmin() {
		return isFromAdmin;
	}

	public void setFromAdmin(boolean isFromAdmin) {
		this.isFromAdmin = isFromAdmin;
	}

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public List<String> getPropertyNames() {
		return propertyNames;
	}

	public void setPropertyNames(List<String> propertyNames) {
		this.propertyNames = propertyNames;
	}

	public List<Long> getFilterIds() {
		if(this.filterIds == null || this.filterIds.size() == 0) {
			return new Vector<Long>();
		}
		return filterIds;
	}

	public void setFilterIds(List<Long> filterIds) {
		this.filterIds = filterIds;
	}

	public String getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}
	
	
}